{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "deb4a99c-3de0-441d-9fcd-d9f373c1e06d",
   "metadata": {},
   "source": [
    "# Bitwise Or\n",
    "\n",
    "The Bitwise Or (denoted as '|') is implemented by applying the following truth table between each pair of qubits (or qubit and bit) in variables A and B.\n",
    "\n",
    "<center>\n",
    "\n",
    "|  a  |  b  | a or b |\n",
    "| :-: | :-: | :----: |\n",
    "|  0  |  0  |   0    |\n",
    "|  0  |  1  |   1    |\n",
    "|  1  |  0  |   1    |\n",
    "|  1  |  1  |   1    |\n",
    "\n",
    "</center>\n",
    "\n",
    "Note that integer and fixed-point numbers are represented in a two-complement method during function evaluation.\n",
    "The binary number is extended in the case of a variable size mismatch.\n",
    "\n",
    "For example, the positive signed number $(110)_2=6$ is expressed as $(00110)_2$ when operating with a five-qubit variable.\n",
    "Similarly, the negative signed number $(110)_2=-2$ is expressed as $(11110)_2$.\n",
    "\n",
    "Examples:\n",
    "\n",
    "5 | 3 = 7 since 101 | 011 = 111\n",
    "\n",
    "5 | -3 = -3 since 0101 | 1101 = 1101\n",
    "\n",
    "-5 | -3 = -1 since 1011 | 1101 = 1111"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c39533b4-a023-4d35-bbb8-9f2fb49be987",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "#### Example 1: Two Quantum Variables\n",
    "\n",
    "This example generates a quantum program that performs bitwise 'or' between two variables.\n",
    "The left arg is a signed with five qubits and the right arg is unsigned with three qubits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "32c35c64-07d7-4244-a2b1-436c983289aa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:22.800526Z",
     "iopub.status.busy": "2024-05-07T13:23:22.799295Z",
     "iopub.status.idle": "2024-05-07T13:23:25.490948Z",
     "shell.execute_reply": "2024-05-07T13:23:25.490160Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate_num,\n",
    "    create_model,\n",
    "    inplace_prepare_int,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
    "    allocate_num(5, True, 0, a)\n",
    "    allocate_num(3, False, 0, b)\n",
    "    inplace_prepare_int(4, a)\n",
    "    inplace_prepare_int(5, b)\n",
    "    res |= a | b\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0eb01ba2-361a-4407-9373-960c6b28c531",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:25.496465Z",
     "iopub.status.busy": "2024-05-07T13:23:25.495033Z",
     "iopub.status.idle": "2024-05-07T13:23:29.372387Z",
     "shell.execute_reply": "2024-05-07T13:23:29.371533Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 4.0, 'b': 5.0, 'res': 5.0}: 1000]\n",
      "{('00100', '101', '10100'): 1000}\n"
     ]
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"bitwise_or_2vars_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "print(result.parsed_counts)\n",
    "print(result.counts_of_multiple_outputs([\"a\", \"b\", \"res\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3e6b1f8-b444-4ec5-af53-16a6e3c46c52",
   "metadata": {},
   "source": [
    "#### Example 2: Integer and Quantum Variable\n",
    "\n",
    "This example generates a quantum program that performs a bitwise 'or' between a quantum variable and an integer.\n",
    "The left arg is an integer equal to three\n",
    "and the right arg is an unsigned quantum variable with three qubits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3c8565b2-eaf1-4315-b748-e128c9f613b8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:29.382548Z",
     "iopub.status.busy": "2024-05-07T13:23:29.382029Z",
     "iopub.status.idle": "2024-05-07T13:23:29.404336Z",
     "shell.execute_reply": "2024-05-07T13:23:29.403683Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, QNum, create_model, prepare_int, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
    "    prepare_int(4, a)\n",
    "    res |= 3 | a\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c9ac0055-0f2a-4a6e-9390-632fd241f28d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:29.408719Z",
     "iopub.status.busy": "2024-05-07T13:23:29.407624Z",
     "iopub.status.idle": "2024-05-07T13:23:31.989764Z",
     "shell.execute_reply": "2024-05-07T13:23:31.989020Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 4.0, 'res': 7.0}: 1000]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"bitwise_or_integer_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
